package definite.Guide.Book.JCheckBox;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JCheckBoxHandlingEvent {

	private static String DESELECTED_LABEL = "Deselected";
	private static String SELECTED_LABEL = "Selected";

	public static void main(String[] args) {

		Runnable runner = new Runnable() {

			@Override
			public void run() {

				JFrame frame = new JFrame("Handling JCheckBox Selected Event");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
				checkBox.setBackground(Color.YELLOW);
				checkBox.setForeground(Color.RED);

				final Color oldbackGround = checkBox.getBackground();
				final Color oldforeGround = checkBox.getForeground();

				// Define ActionListener
				ActionListener actionListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						AbstractButton abstractButton = (AbstractButton) e
								.getSource();
						boolean selected = abstractButton.getModel()
								.isSelected();
						String newLabel = selected ? SELECTED_LABEL
								: DESELECTED_LABEL;
						abstractButton.setText(newLabel);
					}
				};

				// Define ChangeListener
				ChangeListener changeListener = new ChangeListener() {

					@Override
					public void stateChanged(ChangeEvent e) {
						AbstractButton abstractButton = (AbstractButton) e
								.getSource();
						ButtonModel model = abstractButton.getModel();

						boolean armed = model.isArmed();
						boolean pressed = model.isPressed();
						boolean selected = model.isSelected();

						System.out.println("Changed :" + armed + " /" + pressed
								+ " /" + selected);
					}
				};

				// Define ItemListener
				ItemListener itemListener = new ItemListener() {

					@Override
					public void itemStateChanged(ItemEvent e) {
						AbstractButton abstractButton = (AbstractButton) e
								.getSource();
						Color foreGround = abstractButton.getForeground();
						Color backGround = abstractButton.getBackground();
						int state = e.getStateChange();
						if (state == ItemEvent.SELECTED) {
							abstractButton.setBackground(foreGround);
							abstractButton.setForeground(backGround);
						} else if (state == ItemEvent.DESELECTED) {
							abstractButton.setBackground(oldbackGround);
							abstractButton.setForeground(oldforeGround);
						}
					}
				};

				// my code define mouseListener
				MouseListener mouseListener = new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {

						int modifier = e.getModifiers();
						if ((modifier & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK)
							System.out.println("Left Mouse Press");
						if ((modifier & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK)
							System.out.println("Middle Mouse Press");
						if ((modifier & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
							System.out.println("Right Mouse Press");
					}

					@Override
					public void mouseReleased(MouseEvent e) {

						if (SwingUtilities.isLeftMouseButton(e))
							System.out.println("Left Mouse Realease");
						if (SwingUtilities.isRightMouseButton(e))
							System.out.println("Right Mouse Realease");
						if (SwingUtilities.isMiddleMouseButton(e))
							System.out.println("Middle Mouse Realease");
					}
				};

				// add all event to checkBox
				checkBox.addActionListener(actionListener);
				checkBox.addChangeListener(changeListener);
				checkBox.addItemListener(itemListener);
				checkBox.addMouseListener(mouseListener);
				checkBox.setMnemonic(KeyEvent.VK_S); // press ALT + S to see
														// what happen :D :D

				// my code customize the ContentPane .
				JPanel contentPane = (JPanel) frame.getContentPane();
				Border border = BorderFactory
						.createTitledBorder("Handling JCheckBox Event");
				contentPane.setBorder(border);

				frame.add(checkBox, BorderLayout.CENTER);
				frame.setLocationRelativeTo(null);
				frame.setSize(300, 100);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}

}
